package org.awenlice.playerdailyshop.config;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class ShopConfig {
    private static final Logger LOGGER = LogManager.getLogger(); // 添加日志
    public static List<ShopItem> sellShopItems = new ArrayList<>();
    public static List<ShopItem> buyShopItems = new ArrayList<>();
    public static List<weightItem> weightItems = new ArrayList<>();
    public static HashMap<String, List<ShopItem>> sell = new HashMap<>();
    public static HashMap<String, List<ShopItem>> buy = new HashMap<>();
    private int refreshInterval;

    public static void loadConfig(File configFile) {
        // 读取权重信息
        try {
            JsonParser parser = new JsonParser();
            JsonObject json = parser.parse(new FileReader(configFile)).getAsJsonObject();
            Gson gson = new Gson();
            weightItem[] weightItem = gson.fromJson(json.get("weight"), weightItem[].class);
            weightItems.addAll(Arrays.asList(weightItem));
        } catch (Exception e) {
            LOGGER.error("Failed to load weight config!", e); // 日志输出
        }

        // 读取出售商品信息
        try {
            JsonParser parser = new JsonParser();
            JsonObject json = parser.parse(new FileReader(configFile)).getAsJsonObject();
            Gson gson = new Gson();
            ShopItem[] shopItems = gson.fromJson(json.get("SellItems"), ShopItem[].class);
            sellShopItems.addAll(Arrays.asList(shopItems));
        } catch (Exception e) {
            LOGGER.error("Failed to load sellShop config!", e); // 日志输出
        }

        // 读取购买商品信息
        try {
            JsonParser parser = new JsonParser();
            JsonObject json = parser.parse(new FileReader(configFile)).getAsJsonObject();
            Gson gson = new Gson();
            ShopItem[] shopItems = gson.fromJson(json.get("BuyItems"), ShopItem[].class);
            buyShopItems.addAll(Arrays.asList(shopItems));
        } catch (Exception e) {
            LOGGER.error("Failed to load buyShop config!", e); // 日志输出
        }

        // 按照权重将其分类
        for (weightItem weight: weightItems){
            sell.put(weight.name, new ArrayList<>());
            buy.put(weight.name, new ArrayList<>());
        }

        for (ShopItem item: sellShopItems){
            List<ShopItem> target = sell.get(item.weight);
            target.add(item);
            sell.put(item.weight, target);
        }

        for (ShopItem item: buyShopItems){
            List<ShopItem> target = buy.get(item.weight);
            target.add(item);
            buy.put(item.weight, target);
        }

    }

    public static class ShopItem {
        private String id;
        private String nbt; // 存储 NBT 字符串
        private Long price;
        private String weight;

        public ShopItem(String s, String s1, Long i, String s3) {
            this.id = s;
            this.nbt = s1;
            this.price = i;
            this.weight = s3;
        }

        public String getId() {
            return id;
        }

        public CompoundTag getNbt() {
            try {
                // 将 NBT 字符串解析为 CompoundTag
                return TagParser.parseTag(nbt);
            } catch (Exception e) {
                e.printStackTrace();
                return new CompoundTag(); // 返回空的 CompoundTag
            }
        }

        public Long getPrice() {
            return price;
        }

        public String getWeight() {
            return weight;
        }
    }


    public static class weightItem{
        public String name;
        public Double probability;

        public weightItem(String name, double probability) {
            this.name = name;
            this.probability = probability;
        }
    }

    @SerializedName("SellItems")
    ShopItem[] sellItems;

    @SerializedName("BuyItems")
    ShopItem[] buyItems;

    @SerializedName("weight")
    weightItem[] weights;


    public ShopItem[] getSellItems() {
        return sellItems;
    }

    public ShopItem[] getBuyItems() {
        return buyItems;
    }

    public weightItem[] getWeights() {
        return weights;
    }

}